package com.tfjybj.itoo.teach.provider.controller;
/*
  @auther 井朝
  @DESCRIPTION
  @since 1.0.8 2018/11/15
*/

import com.alibaba.druid.util.StringUtils;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.tfjybj.itoo.teach.model.AddCourseInfo;
import com.tfjybj.itoo.teach.model.AddCourseModel;
import com.tfjybj.itoo.teach.model.ChooseCourseModel;
import com.tfjybj.itoo.teach.provider.service.ChooseCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Api(tags = {"选课接口"})
@RequestMapping("/chooseCourse")
@RestController
public class ChooseCourseController{
    @Resource
    ChooseCourseService chooseCourseService;

    /**
     * 查询学生未选课 井朝 2018年11月17日20:22:40
     *
     * @param studentId  学生id
     * @return 未选课的map<list>
     */
    @GetMapping(value = {"/queryUnSelectedCourse/{studentId}"})
    @ApiOperation(value = "查询学生未选课")
    public ItooResult queryUnSelectedCourse(@ApiParam(name = "studentId", value = "学生id", required = true) @PathVariable String studentId) throws IOException, ParseException {
        if (StringUtils.isEmpty(studentId)){
            return ItooResult.build(ItooResult.FAIL, "studentId");
        }
        List<ChooseCourseModel> unChooseCourselist = chooseCourseService.queryUnSelectedCourse (studentId);
        if(unChooseCourselist!=null){
            return ItooResult.build("0000", "查询成功", unChooseCourselist);
        }
        return ItooResult.build("1111", "查询失败", null);
    }

    /**
     * 选课模块-选课 ：井朝 2018年11月23日10:27:11
     * @param addCourseInfo 选课列表
     * @return 选课是否成功
     */
    @PostMapping(value = {"/addCourse"})
    @ApiOperation(value = "选课")
    public ItooResult addCourse(@RequestBody AddCourseInfo addCourseInfo) throws IOException, ParseException {
        List<String> failCourseIds = new ArrayList<>();
//        for(Map.Entry<String,AddCourseModel> addCourseModelEntry : chooseCourseInfo.entrySet()){
        failCourseIds = chooseCourseService.addCourse(addCourseInfo);

        if(failCourseIds.size() <= 0){
            return ItooResult.build("0000", "选课成功", true);
        }
        return ItooResult.build("1111", "选课失败" +failCourseIds);
    }

    /**
     * 查询学生已选课程 井朝 2018年11月15日13:07:34
     *
     * @param studentId  学生id
     * @return 学生已选课信息
     */
    @GetMapping(value = {"/selectStudentSelectedCourse/{studentId}"})
    @ApiOperation(value = "查询学生已选课程")
    public ItooResult selectStudentSelectedCourse(@ApiParam(name = "studentId", value = "学生id", required = true) @PathVariable String studentId)throws IOException{
        if (StringUtils.isEmpty(studentId)){
            return ItooResult.build(ItooResult.FAIL, "studentId");
        }
        List<ChooseCourseModel>chooseCourseModelList;

        chooseCourseModelList = chooseCourseService.selectStudentSelectedCourse(studentId);
        if (chooseCourseModelList != null) {
            return ItooResult.build("0000", "查询学生已选课成功", chooseCourseModelList);
        }
        return ItooResult.build("1111", "查询学生已选课失败", null);
    }

    /**
     * 选课模块-退课 ：井朝 2018年11月24日19:28:22
     *
     * @param model 课程类型:专选 公选
     * @return 是否退选成功
     */
    @PostMapping(value = {"/deleteCourse"})
    @ApiOperation(value = "退课")
    public ItooResult deleteCourse(@RequestBody AddCourseModel model) throws IOException, ParseException{

        boolean flag;
        if (StringUtils.isEmpty(model.getTeachClassId())) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId为空");
        }
        if (StringUtils.isEmpty(model.getCellTimeId())) {
            return ItooResult.build(ItooResult.FAIL, "CellTimeOnDate为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "CourseId为空");
        }
        if (StringUtils.isEmpty(model.getTeacherId())) {
            return ItooResult.build(ItooResult.FAIL, "CourseTeacherId为空");
        }
        if (StringUtils.isEmpty(model.getRoomId())) {
            return ItooResult.build(ItooResult.FAIL, "RoomId为空");
        }
        if (StringUtils.isEmpty(model.getWeekId())) {
            return ItooResult.build(ItooResult.FAIL, "WeekOnDate为空");
        }
        if (StringUtils.isEmpty(model.getStudentId())) {
            return ItooResult.build(ItooResult.FAIL, "getStudentId为空");
        }
        flag = chooseCourseService.deleteCourse(model);
        if(flag){
            return ItooResult.build("0000", "退课成功", true);
        }

        return ItooResult.build("1111", "退课失败" +model);
    }

}